package view_api

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"gin-vue-admin/model/response"
	"gin-vue-admin/service"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"go.uber.org/zap"
)

func CreateUserGoods(c *gin.Context){
	var userGoods model.UserGoods
	_ = c.ShouldBindBodyWith(&userGoods,binding.JSON)
	userGoods.Status = 1
	if err := service.CreateUserGoods(userGoods); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}


func GetUserGoodsList(c *gin.Context) {

	info := request.UserGoodsSearch{}
	_ = c.ShouldBindQuery(&info)
	_,data,total:= service.GetUserGoodsInfoList(info,`top,id desc`)
	dsh,shbh,shtg := service.GetUserGoodsTotalInfo(info)
	response.OkWithDetailed(response.PageResult{
		List:     data,
		Total:    total,
		Page:     info.Page,
		PageSize: info.PageSize,
		Dsh: dsh,
		Shbh: shbh,
		Shtg: shtg,
	}, "获取成功", c)
}

func UpdateUserGoods(c *gin.Context){
	var userGoods model.UserGoods
	_ = c.ShouldBindBodyWith(&userGoods,binding.JSON)
	picList :=userGoods.UserGoodsPic
	userGoods.Status = 1
	service.DeleteUserGoodsPicWithGoodsId(userGoods.ID)
	for _,v := range picList{
		userGoodsPic := model.UserGoodsPic{
			GoodsId:   int(userGoods.ID),
			Pic:       v.Pic,
		}
		service.CreateUserGoodsPic(userGoodsPic)
	}
	userGoods.UserGoodsPic = []model.UserGoodsPic{}
	if err := service.UpdateUserGoods(userGoods); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}


func DeleteUserGoods(c *gin.Context){
	var userGoods model.UserGoods
	_ = c.ShouldBindBodyWith(&userGoods,binding.JSON)
	if err := service.DeleteUserGoods(userGoods); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}